<?php

namespace app\admin\controller\resume;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Resume extends Backend
{
    
    /**
     * UserResume模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('UserResume');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
            ->where($where)
            ->order($sort, $order)
            ->count();
    
            $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
            foreach($list as &$v) {
                $v['sex'] = $v['sex']==1?'男':'女';
                $v['job_status'] = $v['job_status']==1?'在职':'离职';
                $v['status']=$v['status']==1?'已通过':'未通过';

            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $btns = array('refresh');
        
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    public function change_status(){
        $id = request()->route('ids');
        if(empty($id)){
            $result=array('code'=>-1,'msg'=>'缺失参数');
            return json($result);
        }
        
        $list=$this->model->where('resume_id='.$id)->find();
        if($list['status']==1){
            $data['status']=0;
        }else{
            $data['status']=1;
        }
        $edit=$this->model->where('resume_id='.$id)->update($data);
        if($edit){
           return array('code'=>1,'msg'=>'修改成功');
        }else{
            return json(array('code'=>-1,'msg'=>'修改失败'));
        }
        
    }
    public function detail($ids){
        $row = $this->model
            ->alias('r')
            ->join('users u', 'r.user_id = u.user_id')
            ->field('r.*, u.nickname')
            ->where('resume_id', $ids)
            ->find();
        if (!$row) {
            return('该会员不存在，查看是否删除！');
        }

        $row['experience'] = @unserialize($row['experience']);
        $row['id_number_img'] = explode(',', $row['id_number_img']);
        $row['other_photo'] = explode(',', $row['other_photo']);
        if($row['update_time']=='0000-00-00 00:00:00'){
            $row['update_time']=$row['create_time'];
        }
        $row = $row->toArray();
        $count=count($row['experience']);
        $this->view->assign("count", $count);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);

        if (!$row)
            $this->error(__('No Results were found'));

        $adminIds = $this->getDataLimitAdminIds();

        if (is_array($adminIds))
        {
            if (!in_array($row[$this->dataLimitField], $adminIds))
            {
                $this->error(__('You have no permission'));
            }
        }
        $option=DB('option_list')
                ->where('option_id=144 and  is_interest=1')
                ->select();
        $this->assign('option',$option);
        if ($this->request->isPost())
        {
            $resumeModel = model('UserResume');
            $input=input();
            $input['target_work']=implode(",",$input['target_work']);
            $id=$input['ids'];
            unset($input['dialog']);
            unset($input['ids']);

            $re=$resumeModel->where(array('resume_id'=>$id))->update($input);
            if($re){
                $this->success("更改成功");
            } else {
                $this->error("更改失败");
            }
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
